package airthmetic.exercise.mathematics;



/**
 * @Author douhy
 * @description 数学相关算法
 * @date 22/01/20
 */

public class MathDemo {

    // leetCode 263. 丑数
    public static boolean isUgly(int n) {
        if (n <= 0) return false;

        while (n % 2 == 0) {
            n = n / 2;
        }
        while (n % 3 == 0) {
            n = n / 3;
        }
        while (n % 5 == 0) {
            n = n / 5;
        }
        return n == 1;
    }

    // leetCode 9. 回文数
    // 取模10拿最后一位
    // 除10 取模10 拿倒是第二位
    public static boolean isPalindrome(int x) {
        if(x < 0 || (x % 10 == 0 && x != 0)) return false;

        int revertedNumber = 0;
        while (x > revertedNumber){
            revertedNumber = revertedNumber * 10 + x % 10;
            x /= 10;
        }

        return x == revertedNumber || x == revertedNumber / 10;
    }

    public static class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }


    // leetCode 2. 两数相加
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = new ListNode(0);
        ListNode cursor = root;
        int carry = 0;
        while(l1 != null || l2 != null || carry != 0) {
            int l1Val = l1 != null ? l1.val : 0;
            int l2Val = l2 != null ? l2.val : 0;
            int sumVal = l1Val + l2Val + carry;
            carry = sumVal / 10;

            ListNode sumNode = new ListNode(sumVal % 10);
            cursor.next = sumNode;
            cursor = sumNode;

            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }

        return root.next;
    }



    // leetCode 2. 两数相加
    public static ListNode addTwoNumbers02(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;

        ListNode root = new ListNode(0);
        ListNode cursor = root;
        int carry = 0;
        while(l1 != null || l2 != null) {
            int l1Val = l1 != null ? l1.val : 0;
            int l2Val = l2 != null ? l2.val : 0;
            int sumVal = l1Val + l2Val + carry;
            carry = sumVal / 10;

            ListNode sumNode = new ListNode(sumVal % 10);
            cursor.next = sumNode;
            cursor = sumNode;

            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }
        if (carry > 0){
            cursor.next = new ListNode(1);
        }

        return root.next;
    }


    // 461. 汉明距离
    public static int hammingDistance(int x, int y) {
        int num = 0;
        int aligning = 0;
        String aligningZero = "0";
        String xB = Integer.toBinaryString(x);
        String yB = Integer.toBinaryString(y);
        if (x >= y) {
            aligning = xB.length() - yB.length();

            for (int i = 0; i < aligning; i++) {
                yB = aligningZero+yB;
            }
        }else{
            aligning = yB.length() - xB.length();

            for (int i = 0; i < aligning; i++) {
                xB = aligningZero+xB;
            }
        }
        for (int i = 0; i < xB.length(); i++) {
            if ((Integer.valueOf(xB.charAt(i)) ^ Integer.valueOf(yB.charAt(i))) == 1) {
                num++;
            }
        }
        return num;


//        int s = x ^ y, ret = 0;
//        while (s != 0) {
//            ret += s & 1;
//            s >>= 1;
//        }
//        return ret;


    }


    public static void main(String[] args) {
//        System.out.println(isUgly(15));
//        System.out.println(isUgly(18));
//        System.out.println(isUgly(20));
//        System.out.println(isUgly(25));
//        System.out.println(isUgly(24));
//        System.out.println(isPalindrome(12321));

        System.out.println(hammingDistance(1, 4));


    }



}

